package com.catmiao.spark.stream

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
 * @title: SparkStreaming01_WordCount
 * @projectName spark_study
 * @description: TODO
 * @author ChengMiao
 * @date 2024/3/25 00:31
 */
object SparkStreaming06_Transform {

  def main(args: Array[String]): Unit = {


    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkStreaming")
    val ssc = new StreamingContext(sparkConf, Seconds(3))

    val lines: ReceiverInputDStream[String] = ssc.socketTextStream("localhost", 9999)

    /**
     * transform：将底层的rdd获取到后进行操作
     * 使用场景：
     *  - DStream功能不完善
     *  - 需要代码周期性执行
     */
     // Code：Driver端
    val value: DStream[String] = lines.transform(
      rdd => {
        // Code：Driver端【周期性执行，在每一个RDD执行前都会执行】
        rdd.map(
          str => {
            // Code：Executor端
            str
          }
        )
      }
    )

    // Code： Driver端
    lines.map(
      rdd => {
        // Code：Executor端
        rdd
      }
    )

    ssc.start()
    ssc.awaitTermination()
  }


}
